﻿using Newtonsoft.Json.Linq;
using System;

namespace XbmcManager.Modules
{
    public class XbmcPlayer
    {
        private XbmcController xbmc;
        private XbmcConnection connection;

        /// <summary>
        /// Constructor for XbmcPlayer class
        /// </summary>
        internal XbmcPlayer(XbmcController Xbmc, XbmcConnection mConnection)
        {
            this.xbmc = Xbmc;
            this.connection = mConnection;
        }

        /// <summary>
        /// Gets a list of Active Player IDs only. Does not return the type
        /// </summary>
        /// <returns>An Integer array of the active player ids</returns>
        private int[] GetActivePlayerIds()
        {
            JObject response = connection.SendJson("Player.GetActivePlayers");
            if (!XbmcController.isError(response))
            {
                JArray items = (JArray)response["result"];
                int[] playerids = new int[items.Count];
                for (int i = 0; i < items.Count; i++)
                { playerids[i] = (int)items[i].First.First; }
                return playerids;
            }
            else
            {
                return new int[0];
            }
        }

        /// <summary>
        /// Initiate Play.
        /// </summary>
        public void Play()
        {
            int[] PlayerIDs = GetActivePlayerIds();
            foreach (int PlayerID in PlayerIDs)
            {
                JObject args = new JObject();
                args.Add(new JProperty("playerid", PlayerID));
                args.Add(new JProperty("speed", 1));
                connection.SendJson("Player.SetSpeed", args);
            }
        }

        /// <summary>
        /// Initiate Stop.
        /// </summary>
        public void Stop()
        {
            int[] PlayerIDs = GetActivePlayerIds();
            foreach (int PlayerID in PlayerIDs)
            {
                JObject args = new JObject();
                args.Add(new JProperty("playerid", PlayerID));
                connection.SendJson("Player.Stop", args);
            }
        }

        /// <summary>
        /// Initiate Pause.
        /// </summary>
        public void Pause()
        {
            int[] PlayerIDs = GetActivePlayerIds();
            foreach (int PlayerID in PlayerIDs)
            {
                JObject args = new JObject();
                args.Add(new JProperty("playerid", PlayerID));
                args.Add(new JProperty("speed", 0));
                connection.SendJson("Player.SetSpeed", args);
            }
        }

        /// <summary>
        /// Says next item in playlist but actually goes to next chapter in video
        /// </summary>
        public void NextChapter()
        {
            int[] PlayerIDs = GetActivePlayerIds();
            foreach (int PlayerID in PlayerIDs)
            {
                JObject args = new JObject();
                args.Add(new JProperty("playerid", PlayerID));
                connection.SendJson("Player.GoNext", args);
            }
        }

        /// <summary>
        /// Says previous item in playlist but actually goes to next chapter in video
        /// </summary>
        public void PreviousChapter()
        {
            int[] PlayerIDs = GetActivePlayerIds();
            foreach (int PlayerID in PlayerIDs)
            {
                JObject args = new JObject();
                args.Add(new JProperty("playerid", PlayerID));
                connection.SendJson("Player.GoPrevious", args);
            }
        }

        /// <summary>
        /// Attempt to open file and start playing.
        /// Will start video where user left off if possible. 
        /// Else it will start at the beginning.
        /// </summary>
        /// <param name="file"></param>
        public void OpenFile(String file)
        {
            JObject args = new JObject();
            JObject argOption = new JObject();
            JObject item = new JObject();
            item.Add(new JProperty("file", file));
            args.Add(new JProperty("item", item));
            argOption.Add(new JProperty("resume", true));
            args.Add(new JProperty("options", argOption));
            
            connection.SendJson("Player.Open", args);
        }
    }
}
